import axios from '&helpers/axios';
import { reject } from 'lodash';
import { message } from 'antd'
import qs from 'qs'

const API = {
  addOrPutFsm:'/device/updateFsm',
  selectPosition: '/position/selectOne',
  selectIp: '/ip/selectOne',
  selectEquip: '/device/selectEquip'
}
export const models = {
  name: 'setFSMMessagePage',
  model: {
    state: {
      isRight:false,
      FSMneedDatas:{},
      username: "test",
      status: 'add',
      equipInfo: {},
      positionInfo: {},
    },
    reducers: {
      setState(state, payLoad) {
        return {
          ...state,
          ...payLoad
        };
      }
    },
    effects: dispatch => {
      return {
        async putDataFsm(params) {
          console.log(params)
          let formData = {
            imageId: 'pzzcpghe',
            ...params,
          }
          // formData.ImageId = 'pzzcpghe',
          // formData.equipId = 'create'
          const { code, data ,msg} = await axios.post(API.addOrPutFsm,qs.stringify(formData));
          console.log(msg);
          code === 0 && await dispatch.setFSMMessagePage.setState({isRight:true}) && message.success(msg)
          code !== 0 && message.error(msg)
        },
        async putDatasUT(params) {
          const { code, data } = await axios.post(API.addOrPutFsm,qs.stringify(params));
          console.log(code);
          code === 0 && await dispatch.setFSMMessagePage.setState({ ...data }) 
        },
        async getOnePosition(params) {
          const { code, data } = await axios.post(API.selectPosition, qs.stringify(params))
          code === 0 && await dispatch.setFSMMessagePage.setState({ positionInfo: {...data} })
        },
        async getOneEquip(params) {
          const { code, data } = await axios.post(API.selectEquip, qs.stringify(params))
          code === 0 && await dispatch.setFSMMessagePage.setState({ equipInfo: {...data} })
        }
      }
    }
  }
};
